<?php

class AdminHelper
{
	// ------------------------------------------------------------------------------------
	/**
	 * @static
	 * @param  $url
	 * @param  $data
	 * @param  $key
	 * @param  $title
	 * @param  $addon
	 * @return string
	 */
	public static function getColumnTitle($url, $data, $key, $title, $addon = NULL)
	{
		$url = rtrim($url, '/');
		$result = '';
		$result .= '<td' . (!empty($addon) ? ' ' . $addon : NULL) . '>';
		if(!empty($title))
		{
			if(!empty($data['field']) && $data['field'] == $key)
			{
				$result .= '<a class="' . ($data['method'] == 'asc' ? 'a-sorted-asc' : 'a-sorted-desc') .'" href="' . $url . '/sort/' . ($data['method'] == 'asc' ? 'desc' : 'asc') . '/' . $key . '/">' . $title . '</a>';
			}
			else
			{
				$result .= '<a href="' . $url . '/sort/asc/' . $key . '/">' . $title . '</a>';
			}
		}
		else
		{
			$result .= '&nbsp;';
		}
		$result .= '</td>' . "\n";
		
		return $result;
	}

	// ------------------------------------------------------------------------------------
	/**
	 * @static
	 * @param  $type
	 * @param  $filter
	 * @param  $name
	 * @param array $data
	 * @return string
	 */
	public static function getColumnFilter($type, $filter, $name, $data = array())
	{
		$result = '';
		
		switch($type)
		{
			case 'text':
				$result .= '<input type="text" class="i-text" name="filter[' . $name .']" value="' . (!empty($filter[$name]) ? $filter[$name] : NULL ) . '" />';
			break;
			
			case 'select':
				$result .= '<select name="filter[' . $name . ']"><option value="">&nbsp;</option>';
				foreach ($data as $key => $value)
				{
					if(is_array($value))
					{
						$value = $value[0];
					}
					$result .= '<option value="' . $key . '"';
					if(!empty($filter[$name]) && $key == $filter[$name] )
					{
						$result .= 'selected="selected"'; 
					}
					$result .= '>' . $value .'</option>';
				}
				$result .= '</select>';
			break;
			
			default:
				$result .= '&nbsp;';
			break;
		}
		
		$result .= "\n";
		
		return $result;
	}
	
	// ------------------------------------------------------------------------------------
	/**
	 * @static
	 * @param  $name
	 * @param  $filter
	 * @param  $columns
	 * @return void
	 */
	public static function setFilter($name, $filter, $columns)
	{
		$params = array();
		foreach ($filter as $k => $v)
		{
			if(in_array($k, $columns))
			{
				if(!empty($v))
				{
					$params[$k] = $v;
				}
			}
		}
		Yii::app()->user->setState($name . '_filter', $params);
	}
	
	// ------------------------------------------------------------------------------------
	/**
	 * @static
	 * @param  $name
	 * @param  $columns
	 * @return void
	 */
	public static function updateFilter($name, $columns)
	{
		if(isset($_POST['filterOff']))
		{
			self::deleteFilter($name);
		}
		elseif(!empty($_POST['filter']))
		{
			self::setFilter($name, $_POST['filter'], array_keys($columns));
		}
	}
	
	// ------------------------------------------------------------------------------------
	/**
	 * @static
	 * @param  $name
	 * @return 
	 */
	public static function getFilter($name)
	{
		return Yii::app()->user->getState($name . '_filter');
	}
	
	// ------------------------------------------------------------------------------------
	/**
	 * @static
	 * @param  $name
	 * @param  $columns
	 * @return CDbCriteria
	 */
	public static function getFilterCriteria($name, $columns)
	{
		$result = new CDbCriteria;

		$filter = Yii::app()->user->getState($name . '_filter');
		$order = Yii::app()->user->getState($name . '_order');
		if(!empty($order))
		{
			$result->order = '`' . $order['field'] . '`' . ' ' . $order['method'];
		}
		
		if(!empty($filter))
		{
			foreach ($filter as $key => $value)
			{
				switch($columns[$key]['type'])
				{
					case 'integer':
						$result->addColumnCondition(array($key => intval($value)));
					break;
					
					case 'double':
						$result->addColumnCondition(array($key => intval($value)));
					break;
					
					case 'string':
						$result->addSearchCondition($key, $value);
					break;
				}
			}
		}
		
		return $result;
	}
	
	// ------------------------------------------------------------------------------------
	/**
	 * @static
	 * @param  $name
	 * @return void
	 */
	public static function deleteFilter($name)
	{
		Yii::app()->user->setState($name . '_filter', NULL);
	}
	
	// ------------------------------------------------------------------------------------
	/**
	 * @static
	 * @param  $name
	 * @return void
	 */
	public static function setOrder($name)
	{
		$url = explode('/', Yii::app()->request->getPathInfo());
		$order = array(
			'method' => !empty($url[2]) ? $url[2] : NULL,
			'field' => !empty($url[3]) ? $url[3] : NULL
		);
		Yii::app()->user->setState($name . '_order', $order);
	}
	
	// ------------------------------------------------------------------------------------
	/**
	 * @static
	 * @param  $name
	 * @return 
	 */
	public static function getOrder($name)
	{
		return Yii::app()->user->getState($name . '_order');
	}
	
	// ------------------------------------------------------------------------------------
	/**
	 * @static
	 * @param array $params
	 * @return string
	 */
	public static function wysiwyg($params = array())
	{
		$txt =
<<<XXX
<!-- TinyMCE -->
<script type="text/javascript" src="/js/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
// <![CDATA[
	tinyMCE.init({
		// General options
		mode : "specific_textareas",
		editor_selector : "t-wysiwyg",
		theme : "advanced",
		auto_resize : false,
		gecko_spellcheck : true,
		//plugins : "sdpsmcode,safari,style,table,advimage,advlink,iespell,insertdatetime,preview,media,searchreplace,contextmenu,paste,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,inlinepopups",
		plugins : "bbcode",

		// Theme options
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,|,sub,sup,|,charmap,iespell,media,|,fullscreen",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
XXX;
		foreach($params as $key => $value)
		{
			$txt .= $key . ' : ' . $value . ",\n";
		}

		$txt .= '
		SDPsM: "http://sdpsm.com"
	});

	function toggleEditor(id)
	{
		if (!tinyMCE.get(id))
		{
			tinyMCE.execCommand(\'mceAddControl\', false, id);
		}
		else
		{
			tinyMCE.execCommand(\'mceRemoveControl\', false, id);
		}
	}
// ]]>
</script>
<!-- /TinyMCE -->
		';
		return $txt;
	}
	
	// ------------------------------------------------------------------------------------
}